//************************************************************
//	COPYRIGHT 2011 Birthin' Hips - ALL RIGHTS RESERVED
//************************************************************
//
//	$Id: ProducerThread.java$
//
//	$Date: 12/13/11$
//
//	Reason: Gets a random message from MessageCreator, stores it in a verification queue, then sends
//			the message to the read queue to start the message pipeline. Provides functionality for
//			verifying a given message's sequence and content.
//
//*************************************************************

/** $Author: Colton Presler, Ross Kahn, Elysia Haight$ */
/** $Revision: $ */

package com;

import java.util.LinkedList;

public class ProducerThread implements Runnable{

	// A collection of all Messages sent into the queue pipeline. This linked list is
	// treated as a queue itself, being first-in, first-out. This is used to verify
	// not only the content of the messages, but also the ordering.
	private LinkedList<Message> mCollection;
	
	private boolean running;			// True if this thread is still running
	private MessageCreator mFactory;	// Factory that creates random Messages
	private long delay;					// Delay in milliseconds between actions (sending to read queue)
	
	
	/**
	 * Constructor. Instantiates a new verification queue, sets running to true, 
	 * instantiates a new MessageCreator factory, and sets the delay
	 * @param delay		Time in milliseconds that this thread sleeps before 
	 * 					creating a new message and sending it to the queue pipeline
	 */
	//----------------------------------------------------------
	public ProducerThread(long delay){
		mCollection = new LinkedList<Message>();
		running = true;
		mFactory = new MessageCreator();
		this.delay = delay;
	}
	
	/**
	 * Takes a Message that just came off the write queue from the MessageFinalizer (Main thread),
	 * and verifies the order of the Message by using first-in first-out principles. Also verifies
	 * the content of the Message by doing an equals() operation on the Message received against the
	 * Message in the verification queue.
	 * @param m		Message to be verified
	 * @return		True if sequence and content are verified, false if not
	 */
	//----------------------------------------------------------
	public boolean verify(Message m){
		Message queueMessage = mCollection.poll();
		return m.equals(queueMessage);
	}
	
	@Override
	/**
	 * Run method for this thread. Will run forever while running is true. Retreives a randomly
	 * created Message from the MessageCreator, adds it to the verification queue, inserts it into
	 * the read queue, then sleeps for the given delay before repeating.
	 */
	//----------------------------------------------------------
	public void run(){
		while(running){
			Message m = mFactory.createMessage();
			mCollection.addLast(m);
			MessageQueue.insertToReadQueue(m);
			try {
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Sets running to false, which will stop the main loops in run()
	 */
	//----------------------------------------------------------
	public void stop(){
		running = false;
	}
}
